package com.zhonglian.bloodpressure.utils;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.zhonglian.bloodpressure.R;

/**
 * Created : Yx
 * Time : 2018/11/14 10:54
 * Describe :删除通知栏Service
 */
public class CancelNoticeService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int t = intent.getIntExtra("T", 15);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("动态血压检测中..."); //设置通知标题
        builder.setContentText("退出应用将停止检测!");
        builder.setContentInfo("每隔" + t + "分钟");
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder.setAutoCancel(true);
        builder.setWhen(System.currentTimeMillis()); //设置通知时间，默认为系统发出通知的时间，通常不用设置
        builder.setShowWhen(true);
        builder.setNumber(3);
        startForeground(RunService.NOTIFICATION_ID, builder.build());
        // ----开启一条线程，去移除DaemonService弹出的通知
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    // 延迟1s
//                    SystemClock.sleep(1000);
//                    // 取消CancelNoticeService的前台
//                    stopForeground(true);
//                    // 移除DaemonService弹出的通知
//                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//                    manager.cancel(RunService.NOTIFICATION_ID);
//                    // 任务完成，终止自己
//                    stopSelf();
//                }
//            }).start();
        //------end
        return super.onStartCommand(intent, flags, startId);
    }
}